// line = readline()
// var array = JSON.parse(line)

const { log } = require("console");
const { turquoise } = require("color-name");

//树的节点
class TreeNode {
    constructor(val) {
        this.val = val;
        this.left = this.right = null;
    }
}



function convertTree(arr) {
    // 在这里编写转换树的代码，并返回转换完的树
    if (arr.length == 0) {
        return {};
    }

    let root = new TreeNode(arr.shift()); //根节点

    // stack存这一层要遍历的节点 temp存下一层要遍历的节点
    let stack = [root];
    let temp = [];
    while (stack.length > 0) {
        let node = stack.shift(); //拿出栈中的节点

        if (arr.length === 0) { //构建结束
            break;
        }
        let leftVal = arr.shift();
        if (leftVal !== null) {
            node.left = new TreeNode(leftVal);
            temp.push(node.left);
        }
        if (arr.length === 0) { //构建结束
            break;
        }
        let rightVal = arr.shift();
        if (rightVal !== null) {
            node.right = new TreeNode(rightVal);
            temp.push(node.right);
        }
        if (stack.length === 0) { //本层构建结束
            stack = temp;
        }

    }
    return root;

}


let array = [1, 2, null, 3, null, 4]
    // let array = [1, 2, 3]
console.log(JSON.stringify(convertTree(array)))

// console.log('' == 0);